﻿using System.Collections;
using System.Collections.Generic;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using UnityEngine;
using System;
using System.IO;

[Serializable]
public class FixedRoom {
    private List<GMConfig.RoomArrange> _roomArrangeList;
    public List<GMConfig.RoomArrange> roomArrangeList{
        set{
            _roomArrangeList = value;
            //初始化具体id列表
            // if (idList == null) idList = new List<int>(_roomArrangeList.Count);
            for (int i = 0; i < _roomArrangeList.Count; i++)
            {
                if (_roomArrangeList[i] == GMConfig.RoomArrange.None) {
                    if (i >= 0 && i < idList.Count)
                        idList[i] = -1;
                    else 
                        idList.Add(-1);
                }
            }
        }
        get {return _roomArrangeList;}
    }
    public List<int> idList;
	public int width;
	public int height;
	public string name;
	public int id;
    public string theme;
    public string bgm;
    public FixedRoom(int x, int y, string _name, List<GMConfig.RoomArrange> list) { 
        width = x;
		height = y;
		name = _name;
		roomArrangeList = list;
	}
	public FixedRoom(){
        idList = new List<int>();
    }

    public void SetBox(string _theme, string _bgm){
        if (_theme != null)
            theme = _theme;
            
        if (_bgm != null)
            bgm = _bgm;
    }

    public bool IsCompleteSet(){
        for (int i = 0; i < _roomArrangeList.Count; i++)
        {
            if ((int)_roomArrangeList[i] > (int)GMConfig.RoomArrange.None && (int)_roomArrangeList[i] < (int)GMConfig.RoomArrange.Born){
                if (idList[i] == -1) return false;
            }
        }
        return true;
    }

    public FixedRoom DeepClone()
    {
        using (Stream objectStream = new MemoryStream())
        {
            IFormatter formatter = new BinaryFormatter();
            formatter.Serialize(objectStream, this);
            objectStream.Seek(0, SeekOrigin.Begin);
            return formatter.Deserialize(objectStream) as FixedRoom;
        }
    }
}
